package common.post.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import common.customer.bean.Customer;
import common.post.bean.Post;
import common.post.bean.PostComment;
import common.post.bean.SInfo;
import common.post.service.PostCommentService;
import common.post.service.PostService;
import common.post.service.SInfoService;
import common.system.bean.Area;
import common.system.service.AreaService;
import common.system.service.CityService;
import common.util.CommonCode;
import common.util.StrutsUtil;
/**
 * post 相关类,在这里和sinfo配置文件分开,在struts配置文件中没有分开
 * @author 
 *
 */
public class PostAction extends ActionSupport {

	/**
	 * default version id
	 */
	
	private static final long serialVersionUID = 1L;
	private PostCommentService postCommentService;
	private SInfoService sInfoService;
	private PostService postService;
	private AreaService areaService;
	private CityService cityService;
	private int sinfoId;
	private SInfo sinfo;
	private String title;
	private String validationDate;
	//private String desc;
	//private String validationCode;
	private String resultStr;
	private String resultCode;
	private List<Area> areas;
	private List<PostComment> postComments;
	private List<Post> list;//主要帖子数据
	private List<Post> posts;//公告信息
	private String postType;//帖子类型
	private String postToChannel;//公告和活动贴发到系统的哪个
	private String sinfoType;//验证信息的类型
	private String postTitle;//帖子标题
	private String phone;//电话
	private String qq;//qq
	private String qqName;//qq昵称
	private String areaId;//城区
	private String desc;//楼凤信息描述
	private String desc2;//验证经过
	private String validationCode;//验证码
	private int page;//页码
	private int pageSize;//每页大小
	private int total;//总共数量
	private int totalPage;//总共页数
	private Post post;//帖子
	
	private long postId;//帖子ID
	private String replyContent;//帖子回复内容
	private int cityId;//城市ID
	private long commentId;//评论的id
	public String replyPostComment(){
		
		if(commentId<=0){
			return "notfound";
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		Customer user = (Customer)session.getAttribute("user");
		if(this.postCommentService.addCommentReply(commentId, replyContent, user)){			
			return SUCCESS;
		}else{
			resultStr ="保存帖子回复信息发生错误";
			resultCode = "0";
			return "error";
		}
	}
	public String reply(){
		if(postId<=0){
			return "notfound";
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		Customer user = (Customer)session.getAttribute("user");
		if(this.postCommentService.addPostComment(postId, replyContent, user)){			
			return SUCCESS;
		}else{
			resultStr ="保存帖子回复信息发生错误";
			resultCode = "0";
			return "error";
		}
	}
	//获得所有技术贴
	public String getTechniquePostList(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		int currentCityId =0;
		//当前城市
		if(cityId==0){			
			currentCityId = StrutsUtil.getCurrentCityBySession(session);
		}else{
			currentCityId =cityId;
		}
		//获得总量
		total = (int)this.postService.countPostByType(CommonCode.POST_TYPE_OTHER,currentCityId);
		
		if(pageSize <= 0){
			pageSize=20;
		}
		totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
		if(page<1){
			page=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		//获得列表
		list = this.postService.getPostByType(CommonCode.POST_TYPE_OTHER,currentCityId, page, pageSize);
		posts = this.postService.getGonggaoByTypeAndChannel(CommonCode.POST_TYPE_GONGGAO,CommonCode.SYSTEM_CHANNEL_TIEBA);
		return SUCCESS;
	}
	public String getPostById(){
		if(postId<=0){
			return "notfound";
		}
		post = this.postService.getPostById(postId);
		if(post==null){
			return "notfound";
		}
		total = (int)this.postCommentService.getPostCommentCountBYPostId(postId);
		if(pageSize <= 0){
			pageSize=10;
		}
		totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
		if(totalPage==0){
			totalPage=1; 
		}
		if(page<1){
			page=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		postComments = this.postCommentService.getPostCommentsBYPostId(postId, page, pageSize);
		return SUCCESS;
	}
	
	public String addSinfoComment(){
		return SUCCESS;
	}
	//发布普通帖,各种贴
	public String publishPost(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		int currentCityId = StrutsUtil.getCurrentCityBySession(session);
		areas = areaService.getAreaByCityId(currentCityId);
		return SUCCESS;
	}
	/**
	 * 发帖通用接口
	 * @return
	 */
	public String addCommonPost(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		int currentCityId = StrutsUtil.getCurrentCityBySession(session);
		if(postType==null || "".equals(postType.trim())){
			resultStr ="帖子类型必须选择!";
			resultCode = "0";
			areas = areaService.getAreaByCityId(currentCityId);
			return "validationFail";
		}else if(CommonCode.POST_TYPE_GONGGAO.equals(postType.trim())){
			Customer user =(Customer)session.getAttribute("user"); 
			if(!CommonCode.CUSTOMER_ROLE_ADMIN.equals(user.getUserRole()) && !CommonCode.CUSTOMER_ROLE_MODERATOR.equals(user.getUserRole())){
				areas = areaService.getAreaByCityId(currentCityId);
				resultStr ="只有管理员或者版主才能发帖!";
				resultCode = "0";
				return "validationFail";
			}
			//---------------------公告
			if(postToChannel==null || "".equals(postToChannel.trim()) || "0".equals(postToChannel.trim())){
				areas = areaService.getAreaByCityId(currentCityId);
				resultStr ="发送版块必须选择!";
				resultCode = "0";
				return "validationFail";
			}
			if(CommonCode.CUSTOMER_ROLE_MODERATOR.equals(user.getUserRole())){
				if(user.getSystemChannel()==null || user.getSystemChannel().equals(postToChannel.trim())){
					areas = areaService.getAreaByCityId(currentCityId);
					resultStr ="你只能在自己的板块发布公告!";
					resultCode = "0";
					return "validationFail";
				}
			}
			if(postTitle==null || "".equals(postTitle.trim())){
				areas = areaService.getAreaByCityId(currentCityId);
				resultStr ="帖子标题必须填写!";
				resultCode = "0";
				return "validationFail";
			}
			String validationCodeSession= ServletActionContext.getRequest().getSession().getAttribute("validationCode").toString(); 
			if(validationCodeSession==null || "".equals(validationCodeSession.trim())){
				resultStr ="验证码不存在,请重新换一个验证码";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(!validationCodeSession.equals(validationCode)){
				resultStr ="验证码输入有误,请重新输入";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			Post post = new Post();
			post.setCity(cityService.getCityByID(currentCityId));
			post.setContent(desc);
			post.setCustomer((Customer)ServletActionContext.getRequest().getSession().getAttribute("user"));
			post.setPublishTime(System.currentTimeMillis());
			post.setTitle(postTitle);
			post.setType(postType);
			post.setChannel(postToChannel);
			long id = this.postService.addPost(post);
			if(id<=0){
				return "error";
			}else{
				return SUCCESS;
			}
		}else if(CommonCode.POST_TYPE_HUODONG.equals(postType.trim())){
			//---------------------活动
			Customer user =(Customer)session.getAttribute("user"); 
			if(!CommonCode.CUSTOMER_ROLE_ADMIN.equals(user.getUserRole()) && !CommonCode.CUSTOMER_ROLE_MODERATOR.equals(user.getUserRole())){
				areas = areaService.getAreaByCityId(currentCityId);
				resultStr ="只有管理员或者版主才能发帖!";
				resultCode = "0";
				return "validationFail";
			}
			if(postToChannel==null || "".equals(postToChannel.trim()) || "0".equals(postToChannel.trim())){
				areas = areaService.getAreaByCityId(currentCityId);
				resultStr ="发送版块必须选择!";
				resultCode = "0";
				return "validationFail";
			}
			if(postTitle==null || "".equals(postTitle.trim())){
				areas = areaService.getAreaByCityId(currentCityId);
				resultStr ="帖子标题必须填写!";
				resultCode = "0";
				return "validationFail";
			}
			String validationCodeSession= ServletActionContext.getRequest().getSession().getAttribute("validationCode").toString(); 
			if(validationCodeSession==null || "".equals(validationCodeSession.trim())){
				resultStr ="验证码不存在,请重新换一个验证码";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(!validationCodeSession.equals(validationCode)){
				resultStr ="验证码输入有误,请重新输入";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			Post post = new Post();
			post.setCity(cityService.getCityByID(currentCityId));
			post.setContent(desc);
			post.setCustomer((Customer)ServletActionContext.getRequest().getSession().getAttribute("user"));
			post.setPublishTime(System.currentTimeMillis());
			post.setTitle(postTitle);
			post.setType(postType);
			post.setChannel(postToChannel);
			long id = this.postService.addPost(post);
			if(id<=0){
				return "error";
			}else{
				return SUCCESS;
			}
		}else if(CommonCode.POST_TYPE_TOUSU.equals(postType.trim())){
			//---------------------投诉
			if(postTitle==null || "".equals(postTitle.trim())){
				areas = areaService.getAreaByCityId(currentCityId);
				resultStr ="帖子标题必须填写!";
				resultCode = "0";
				return "validationFail";
			}
			String validationCodeSession= ServletActionContext.getRequest().getSession().getAttribute("validationCode").toString(); 
			if(validationCodeSession==null || "".equals(validationCodeSession.trim())){
				resultStr ="验证码不存在,请重新换一个验证码";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(!validationCodeSession.equals(validationCode)){
				resultStr ="验证码输入有误,请重新输入";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			Post post = new Post();
			post.setCity(cityService.getCityByID(currentCityId));
			post.setContent(desc);
			post.setCustomer((Customer)ServletActionContext.getRequest().getSession().getAttribute("user"));
			post.setPublishTime(System.currentTimeMillis());
			post.setTitle(postTitle);
			post.setType(postType);
			post.setChannel(CommonCode.SYSTEM_CHANNEL_TOUSU);
			long id = this.postService.addPost(post);
			if(id<=0){
				return "error";
			}else{
				return SUCCESS;
			}
		}else if(CommonCode.POST_TYPE_YANZHENG.equals(postType.trim())){
			//----------------------验证贴-----------
			
			//验证贴需要和具体的sinfo关联
			if(postType==null || "".equals(postType.trim()) || "0".equals(postType.trim())){
				resultStr ="帖子类型必须选择";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(sinfoType==null || "".equals(sinfoType.trim()) || "0".equals(sinfoType.trim())){
				resultStr ="信息类型必须选择";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(postTitle==null || "".equals(postTitle.trim())){
				resultStr ="帖子标题必须填写";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(phone==null || "".equals(phone.trim())){
				resultStr ="联系人电话必须填写";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(qq==null || "".equals(qq.trim())){
				resultStr ="联系人QQ必须填写";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(qqName==null || "".equals(qqName.trim())){
				resultStr ="联系人QQ昵称必须填写";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(areaId==null || "".equals(areaId.trim()) || "0".equals(areaId.trim())){
				resultStr ="城区必须选择";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			String validationCodeSession= ServletActionContext.getRequest().getSession().getAttribute("validationCode").toString(); 
			if(validationCodeSession==null || "".equals(validationCodeSession.trim())){
				resultStr ="验证码不存在,请重新换一个验证码";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(!validationCodeSession.equals(validationCode)){
				resultStr ="验证码输入有误,请重新输入";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			//在这里关联sinfo
			this.postService.publishValidationPost(postType,sinfoType,
					postTitle,phone, qq, qqName,areaId,desc,desc2,CommonCode.SYSTEM_CHANNEL_YANZHENG,cityService.getCityByID(currentCityId),(Customer)ServletActionContext.getRequest().getSession().getAttribute("user"));
			return SUCCESS;
		}else if(CommonCode.POST_TYPE_OTHER.equals(postType.trim())){
			//----------------------海阔天空
			if(postTitle==null || "".equals(postTitle.trim())){
				areas = areaService.getAreaByCityId(currentCityId);
				resultStr ="帖子标题必须填写!";
				resultCode = "0";
				return "validationFail";
			}
			String validationCodeSession= ServletActionContext.getRequest().getSession().getAttribute("validationCode").toString(); 
			if(validationCodeSession==null || "".equals(validationCodeSession.trim())){
				resultStr ="验证码不存在,请重新换一个验证码";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			if(!validationCodeSession.equals(validationCode)){
				resultStr ="验证码输入有误,请重新输入";
				resultCode = "0";
				areas = areaService.getAreaByCityId(currentCityId);
				return "validationFail";
			}
			Post post = new Post();
			post.setCity(cityService.getCityByID(currentCityId));
			post.setContent(desc);
			post.setCustomer((Customer)ServletActionContext.getRequest().getSession().getAttribute("user"));
			post.setPublishTime(System.currentTimeMillis());
			post.setTitle(postTitle);
			post.setType(postType);
			post.setChannel(CommonCode.SYSTEM_CHANNEL_OTHER);//海阔天空频道
			long id = this.postService.addPost(post);
			if(id<=0){
				return "error";
			}else{
				return SUCCESS;
			}
		}else{
			resultStr ="未知帖子类型!";
			resultCode = "0";
			areas = areaService.getAreaByCityId(currentCityId);
			return "validationFail";
		}
	}
	//发验证贴
	public String addValidationPost(){
		if(validationDate==null || "".equals(validationDate.trim())){
			resultStr ="验证贴验证日期必需填写";
			resultCode = "0";
			return "validationFail";
		}
		if(title==null || "".equals(title.trim())){
			resultStr ="验证贴标题必需填写";
			resultCode = "0";
			return "validationFail";
		}
		String validationCodeSession= ServletActionContext.getRequest().getSession().getAttribute("validationCode").toString(); 
		if(validationCodeSession==null || "".equals(validationCodeSession.trim())){
			resultStr ="验证码不存在,请重新换一个验证码";
			resultCode = "0";
			return "validationFail";
		}
		if(!validationCodeSession.equals(validationCode)){
			resultStr ="验证码输入有误,请重新输入";
			resultCode = "0";
			//System.out.println(validationCodeSession+"="+validationCode);
			return "validationFail";
		}
		if(sinfoId==0){
			resultStr ="找不到对应的楼凤,请重试";
			resultCode = "0";
			return "validationFail";
		}
		sinfo=this.sInfoService.getSInfoById(sinfoId);
		Post post =new Post();
		
		post.setArea(sinfo.getArea());
		post.setCity(sinfo.getCity());
		post.setContent(desc);
		Customer customer = (Customer)ServletActionContext.getRequest().getSession().getAttribute("user");
		post.setCustomer(customer);
		post.setInfo(sinfo);
		post.setPublishTime(System.currentTimeMillis());
		post.setTitle(title);
		post.setType(CommonCode.POST_TYPE_YANZHENG);
		post.setValidationDate(validationDate);
		long id = this.postService.addPost(post);
		if(id<=0){
			return ERROR;
		}
		return SUCCESS;
	}
	//发验证贴首页
	public String validationPostIndex(){
		System.out.println("sinfo的sInfoId为:"+sinfoId);
		if(sinfoId==0){
			return "notfound";
		}
		sinfo=this.sInfoService.getSInfoById(sinfoId);
		String[] phps = sinfo.getPics()==null?null:sinfo.getPics().split(",");
		System.out.println(phps);
		List<String> photos = new ArrayList<String>();
		if(phps!=null){
			for(String s:phps){
				photos.add(s);
			}
		}
		sinfo.setPhotos(photos);
		return SUCCESS;
	}
	//=================getter and setter method==================
	public int getSinfoId() {
		return sinfoId;
	}
	public void setSinfoId(int sinfoId) {
		this.sinfoId = sinfoId;
	}
	public SInfo getSinfo() {
		return sinfo;
	}
	public void setSinfo(SInfo sinfo) {
		this.sinfo = sinfo;
	}
	public SInfoService getsInfoService() {
		return sInfoService;
	}
	public void setsInfoService(SInfoService sInfoService) {
		this.sInfoService = sInfoService;
	}
	public PostService getPostService() {
		return postService;
	}
	public void setPostService(PostService postService) {
		this.postService = postService;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getValidationDate() {
		return validationDate;
	}
	public void setValidationDate(String validationDate) {
		this.validationDate = validationDate;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public String getValidationCode() {
		return validationCode;
	}
	public void setValidationCode(String validationCode) {
		this.validationCode = validationCode;
	}
	public String getResultStr() {
		return resultStr;
	}
	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}
	public String getResultCode() {
		return resultCode;
	}
	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}
	public List<Area> getAreas() {
		return areas;
	}
	public void setAreas(List<Area> areas) {
		this.areas = areas;
	}
	public AreaService getAreaService() {
		return areaService;
	}
	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}
	public String getPostType() {
		return postType;
	}
	public void setPostType(String postType) {
		this.postType = postType;
	}
	public String getPostToChannel() {
		return postToChannel;
	}
	public void setPostToChannel(String postToChannel) {
		this.postToChannel = postToChannel;
	}
	public String getSinfoType() {
		return sinfoType;
	}
	public void setSinfoType(String sinfoType) {
		this.sinfoType = sinfoType;
	}
	public String getPostTitle() {
		return postTitle;
	}
	public void setPostTitle(String postTitle) {
		this.postTitle = postTitle;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public String getQqName() {
		return qqName;
	}
	public void setQqName(String qqName) {
		this.qqName = qqName;
	}
	public String getAreaId() {
		return areaId;
	}
	public void setAreaId(String areaId) {
		this.areaId = areaId;
	}
	public String getDesc2() {
		return desc2;
	}
	public void setDesc2(String desc2) {
		this.desc2 = desc2;
	}
	public CityService getCityService() {
		return cityService;
	}
	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}
	public Post getPost() {
		return post;
	}
	public void setPost(Post post) {
		this.post = post;
	}
	public long getPostId() {
		return postId;
	}
	public void setPostId(long postId) {
		this.postId = postId;
	}

	public PostCommentService getPostCommentService() {
		return postCommentService;
	}

	public void setPostCommentService(PostCommentService postCommentService) {
		this.postCommentService = postCommentService;
	}

	public List<PostComment> getPostComments() {
		return postComments;
	}

	public void setPostComments(List<PostComment> postComments) {
		this.postComments = postComments;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public List<Post> getList() {
		return list;
	}
	public void setList(List<Post> list) {
		this.list = list;
	}
	public List<Post> getPosts() {
		return posts;
	}
	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}
	public int getCityId() {
		return cityId;
	}
	public void setCityId(int cityId) {
		this.cityId = cityId;
	}
	public String getReplyContent() {
		return replyContent;
	}
	public void setReplyContent(String replyContent) {
		this.replyContent = replyContent;
	}
	public long getCommentId() {
		return commentId;
	}
	public void setCommentId(long commentId) {
		this.commentId = commentId;
	}
}
